package com.jason.rtsp.runner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @创建人 zhouhx
 * @创建时间 2023/8/31 15:00
 * @描述 视频转换器
 */
@Slf4j
@Component
public class VideoConvertorRunner implements ApplicationRunner {

    public Process process;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        convertVideoToRstp("/Users/zhouhaoxiang/Downloads/20230303.mp4","http://127.0.0.1:8080/receive");
    }

    public void convertVideoToRstp(String videoId,String videoPath){
        String ffmpeg = System.getProperty("ffmpeg");
        if(process!=null){
            process.destroy();
        }
        StringBuffer cmd = new StringBuffer(ffmpeg);
        cmd.append(" -i \"")
                .append(videoId)
                .append("\"")
                .append(" -q 0 -f mpegts -codec:v mpeg1video -s 800x600 ")
                .append(videoPath);
        log.info("mmpeg推流命令："+cmd);
        try {
            process = Runtime.getRuntime().exec(cmd.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
